from microbit import *
import math
bta=button_a
btb=button_b
ac=accelerometer
cm=compass

def is_button_pressed(b):
    if b == 'a':
        return bta.is_pressed()
    if b == 'b':
        return btb.is_pressed()
    return bta.is_pressed() and btb.is_pressed()

def is_logo_touched():
    return pin_logo.is_touched()

def get_sound_level():
    return microphone.sound_level()

def accelerometer_is_gesture(g):
    return ac.is_gesture(g)

def get_accelerometer_value(o):
    if o == 'X':
        return ac.get_x()
    elif o == 'Y':
        return ac.get_y()
    elif o == 'Z':
        return ac.get_z()
    return ac.get_strength()

def get_accelerometer_rotation(d):
    x, y, z = ac.get_values()
    if d == 'pitch':
        r = math.atan2(y, -z)
    else:
        r = math.atan2(x, -z)
    angle = math.floor((180 * r) / math.pi)
    if angle == -180:
        angle = 0
    return angle

def get_compass_magnetic(o):
    if o == 'X':
        return cm.get_x()
    if o == 'Y':
        return cm.get_y()
    if o == 'Z':
        return cm.get_z()
    return cm.get_field_strength()

def get_temperature():
    return temperature()

# 映射函数
def map_value(value, in_min, in_max, out_min, out_max):
    in_range = in_max - in_min
    normalized_value = (value - in_min) / in_range
    out_range = out_max - out_min
    mapped_value = (normalized_value * out_range) + out_min
    return mapped_value